*&---------------------------------------------------------------------*
*& Report ztest_salv01
*&---------------------------------------------------------------------*

report ztest_salv01.

class lcl_event_handler definition.
  public section.
    methods on_link_click
      for event link_click of cl_salv_events_table
      importing row column.

endclass.

class lcl_event_handler implementation.
  method on_link_click.
    "Handle hotspot click
    data(lv_msg) = | Row: { row } Column: { column } |.
    message lv_msg type 'I'.
  endmethod.
endclass.

start-of-selection.
  select * from spfli up to 20 rows
  into table @data(lt_spfli).

  try.
      cl_salv_table=>factory(
      importing
        r_salv_table   = data(lo_salv)
        changing
          t_table        = lt_spfli
      ).

      " 显示工具栏
      lo_salv->get_functions( )->set_all(  ).

      " 条纹显示
      data(lo_display_settings) = lo_salv->get_display_settings(  ).
      lo_display_settings->set_striped_pattern( cl_salv_display_settings=>true ).

      " 设置ALV表头
      lo_display_settings->set_list_header( '航班' ).

      " 字段名设置
      data(lo_column) = lo_salv->get_columns(  )->get_column( 'CONNID' ).
      lo_column->set_short_text( '航班' ).
      lo_column->set_medium_text( '航班' ).
      lo_column->set_long_text( '航班' ).


      " 自适应列宽
      lo_salv->get_columns(  )->set_optimize( 'X' ).

      " 隐藏某列
      data(lo_hide_col) = lo_salv->get_columns(  )->get_column( 'PERIOD' ).
      lo_hide_col->set_visible( abap_false ).

      " 按列排序
      lo_salv->get_sorts( )->add_sort( 'CONNID' ).

*----------------------------------------------
*     Layut saving
*----------------------------------------------
      data(lo_layout) = lo_salv->get_layout( ).
      " set layout key
      lo_layout->set_key( value #( report = sy-repid ) ).

      " Allow variant saving
      " remove restriction on saving layouts
      lo_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).

      " Set initial layout
      lo_layout->set_initial_layout( 'Z01' ).

      " Allow setting layouts as default layouts
      lo_layout->set_default( abap_true ).

*-----------------------------------------------
*   热点
*-----------------------------------------------
      " Register hotspot column
      data(lo_hotspot_col) = lo_salv->get_columns(  )->get_column( 'CARRID' ).
      data(lo_hotspot_table) = cast cl_salv_column_table( lo_hotspot_col ).
      lo_hotspot_table->set_cell_type( if_salv_c_cell_type=>hotspot ).

      " Register hotspot click handler
      data(lo_event_handler) = new lcl_event_handler(  ).
      data(lo_events) = lo_salv->get_event( ).
      set handler lo_event_handler->on_link_click for lo_events.

      " 合计列
      lo_salv->get_aggregations( )->add_aggregation(
        columnname  = 'DISTANCE'
        aggregation = if_salv_c_aggregation=>total ).

      " 显示ALV
      lo_salv->display(  ).

    catch cx_salv_msg into data(lo_exception).
      data(lv_msg) = lo_exception->get_text(  ).
      message lv_msg type 'I'.
  endtry.


